package com._520it.day2._06_template.crud;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com._520it.day2._06_template.BeanHandler;
import com._520it.day2._06_template.BeanListHandler;
import com._520it.day2._06_template.IResultSetHandler;
import com._520it.day2._06_template.JDBCTemplate;

//步骤：贾琏欲执事
public class StudentDAOImpl implements IStudentDAO {

	@Override
	public void add(Student stu) {
		String sql = "INSERT INTO t_student (name,age) VALUES(?,?)";
		JDBCTemplate.update(sql, stu.getName(), stu.getAge());
	}

	@Override
	public void delete(Long id) {
		String sql = "DELETE FROM t_student WHERE id=?";
		JDBCTemplate.update(sql, id);
	}

	@Override
	public void update(Student newStu) {
		String sql = "UPDATE t_student SET name=?,age=? WHERE id=?";
		JDBCTemplate.update(sql, newStu.getName(), newStu.getAge(), newStu.getId());
	}

	@Override
	public Student get(Long id) {
		
		String sql = "SELECT * FROM t_student WHERE id=?";
		return JDBCTemplate.query(sql, new BeanHandler<>(Student.class), id);
	}

	@Override
	public List<Student> list() {
		String sql = "SELECT * FROM t_student";
		return JDBCTemplate.query(sql,new BeanListHandler<>(Student.class));
	}
	
/*	//定义一个类，专门用于处理t_student表的结果集
	class StudentResultSetHandle implements IResultSetHandler<List<Student>>{

		@Override
		public List<Student> handle(ResultSet rs) throws SQLException {
			List<Student> list = new ArrayList<>();
			while (rs.next()) {
				Student stu = new Student();
				list.add(stu);
				stu.setId(rs.getLong("id"));
				stu.setName(rs.getString("name"));
				stu.setAge(rs.getInt("age"));
			}
			return list;
		}
		
	}*/
}
